<?php
class ControllerSoftStatistics extends Controller
{
	function index()
	{	
		$dt = getdate();
		$year = $dt['year'];
		$this->data['curentyear'] = $dt['year'];
		$this->data['curentmonth'] = $dt['mon'];
		for($i= $year - 10;$i<$year +10;$i++)
			$this->data["Year"][] =$i;
		for($i=1;$i<=12;$i++)
			$this->data["Month"][] =$i;
		$this->id='content';
		$this->template='soft/statistics.tpl';
		$this->layout='layout/center';
		$this->render();
	}
	
	public function getList()
	{
		$startdate=$this->date->formatViewDate($this->request->get['startdate']);
		$enddate=$this->date->formatViewDate($this->request->get['enddate']);
		$type=$this->request->get['type'];
		$template=$this->request->get['template'];
		$this->getListOrder($startdate,$enddate,$type);
		
		$this->template=$template.'.tpl';
		$this->render();
	}
	
	public function getBalance()
	{
		$startdate=$this->date->formatViewDate($this->request->get['startdate']);
		$enddate=$this->date->formatViewDate($this->request->get['enddate']);
		$this->getListOrder("",$enddate,"sale");
		$this->data['saletatol'] = $this->data['ordersum'];
		$this->getListOrder("",$enddate,"export");
		$this->data['exporttatol'] = $this->data['ordersum'];
		$this->getListOrder("",$enddate,"import");
		$this->data['importtatol'] = $this->data['ordersum'];
		$this->data['output'] = $this->string->numberFormate($this->data['saletatol'] + $this->data['exporttatol'] - $this->data['importtatol']);
		$this->template='common/output.tpl';
		$this->render();
	}
	
	function getListOrder($startdate,$enddate,$type)
	{
		$this->load->model("addon/order");
		$wherestart = "";
		$whereend = "";
		if($this->request->get['startdate'] !="")
			$wherestart = "AND orderdate >= '".$startdate."' ";
		if($this->request->get['enddate'] !="")
			$whereend = "AND orderdate  <= '".$enddate."'";
		$where = " AND `type` = '".$type."' 
					".$wherestart."
					".$whereend."
					ORDER BY  `order`.`orderdate` DESC ";
		
		$list = $this->model_addon_order->getListOrder($where);
		$this->data['orders'] = array();
		$this->data['ordersum'] = 0;
		
		foreach($list as $key => $item)
		{
			$this->data['orders'][$key] = $this->model_addon_order->getItem($item['orderid']);
			$this->data['ordersum']+= $this->data['orders'][$key]['sum'];
		}	
	}
	
	public function statisticsProduct()
	{
		$startdate=$this->date->formatViewDate($this->request->get['startdate']);
		$enddate=$this->date->formatViewDate($this->request->get['enddate']);
		$type=$this->request->get['type'];
		$this->load->model("addon/order");
		$where = " AND `type` = '".$type."' 
					AND orderdate >= '".$startdate."' 
					AND orderdate  <= '".$enddate."' 
					ORDER BY  `order`.`orderdate` DESC ";
		
		$list = $this->model_addon_order->getListOrder($where);
		
	}
	
	public function instock()
	{
		$this->id='content';
		$this->template='soft/instock.tpl';
		$this->layout='layout/center';
		$this->render();
	}
	
	public function productInstock()
	{
		$this->load->model("core/media");
		$this->load->model("core/sitemap");
		$sitemap = $this->model_core_sitemap->getListByParent("category",$this->user->getSiteId(),"Active");
		$arr = $this->string->matrixToArray($sitemap,"sitemapid");
		$list= "'".implode("','",$arr)."'";
		$where = " AND refersitemap in (".$list.")";
		$this->data['medias'] = $this->model_core_media->getList($where);
		$this->template='soft/product.tpl';
		$this->render();
	}
}
?>